package com.xk.demo.department.service.impl;

import com.xk.demo.department.entity.SearchDepartmentEntity;
import com.xk.demo.department.respository.DepartmentDao;
import com.xk.demo.department.service.DepartmentService;
import com.xk.demo.mybatis.entity.PtDepartmentEntity;
import com.xk.demo.mybatis.entity.PtUserEntity;
import com.xk.demo.mybatis.flexigrid.PadingRstType;
import com.xk.demo.mybatis.flexigrid.PagingBean;
import com.xk.demo.user.entity.UserSearchEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.ILoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    //没有父类部门，则BELONG_CENTER设为null
    private final static String DEPARTMENT_BELONG_CENTER_NO_PARENT =null;

    private Logger logger =  LogManager.getLogger(this.getClass().getName());
    @Resource
    private DepartmentDao departmentDao;

    @Override
    public PadingRstType<PtDepartmentEntity> getDepartmentInfoListByPage(SearchDepartmentEntity search, PagingBean paging) {
        paging.deal(PtDepartmentEntity.class);
        PadingRstType<PtDepartmentEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(paging.getPage());
        List<PtDepartmentEntity> list = departmentDao.queryDepartmentListByPage(search,paging);
        Integer total = departmentDao.queryDepartmentListTotal(search);
        padingRstType.setTotal(total);
        padingRstType.setRawRecords(list);
        padingRstType.putItems();
        return padingRstType;
    }

    @Override
    public void deleteDepartmentById(String depUuid) {
        departmentDao.deleteDepartmentById(depUuid);
    }


    @Override
    public String updatePtDepartmentBelongCenter(String branchName) {
        PtDepartmentEntity ptDepartmentEntity = departmentDao.selectPtDepartmentByBranchName(branchName);
        logger.info("BelongCenter" + ptDepartmentEntity.getBelongCenter());
        if (ptDepartmentEntity.getBelongCenter().equals("-1")) {
            return  "集团测不可以绑定";
        }else if(ptDepartmentEntity.getBelongCenter().isEmpty()){

        }
        return "存在父部门,不能绑定";

    }

    @Override
    public String clearBoundPtDepartmentByBranchName(String branchName) {
        PtDepartmentEntity ptDepartmentEntity = departmentDao.selectPtDepartmentByBranchName(branchName);
        List<PtDepartmentEntity> ptDepartmentEntities = departmentDao.selectAllDepartment();
        Integer index =0;
        for(PtDepartmentEntity ptDepartmentEntity1:ptDepartmentEntities){
            if(ptDepartmentEntity.getDepUuid().equals(ptDepartmentEntity1.getBelongCenter())){
                logger.info(ptDepartmentEntity1.getBelongCenter());
                ptDepartmentEntity1.setBelongCenter(null);
                index++;
            }
        }
        if(index==0){
            return ptDepartmentEntity.getBranchName()+"没有子类部门";
        }else{
            return "已解绑";
        }

    }


}
